<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT * FROM "
        . tablename('bontrip_line') . " WHERE uniacid = {$uniacid} and deleted_at is null"
        . " ORDER BY id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM "
        . tablename('bontrip_line') . " WHERE uniacid = {$uniacid} and deleted_at is null");
    $pager = pagination($total, $pindex, $psize);

} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    $line = pdo_fetch("select * from " . tablename('bontrip_line') . " where id={$id} and uniacid={$uniacid}");
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $uniacid,
            'sort' => intval($_GPC['sort']),
            'tag' => $_GPC['tag'],
            'label' => str_replace('，', ',', $_GPC['label']),
            'name' => $_GPC['name'],
            'thumb' => $_GPC['thumb'],
            'thumbs' => serialize($_GPC['thumbs']),
            'remark' => $_GPC['remark'],
            'introduction' => $_GPC['introduction'],
            'price' => $_GPC['line_price'],
            'updated_at' => TIMESTAMP
        );

        $product = $_GPC['product'];
        $price = $_GPC['price'];
        $use = $_GPC['use'];
        $buy = $_GPC['buy'];
        //重置索引
        if(!empty($product["trip"])){
            $product['trip'] = array_values($product["trip"]);
        }
        if(!empty($price["price"])){
            $price['price'] = array_values($price["price"]);
        }
        if(!empty($buy["buy"])){
            $buy['buy'] = array_values($buy["buy"]);
        }
        $data['product_desc'] = serialize($product);
        $data['price_desc'] = serialize($price);
        $data['buy_desc'] = serialize($buy);
        if (!empty($id)) {
            pdo_update('bontrip_line', $data, array('id' => $id));
        } else {
            $data['created_at'] = $data['updated_at'];
            pdo_insert('bontrip_line', $data);
            $id = pdo_insertid();
        }
        pdo_delete('bontrip_line_hotel', array('line_id' => $id));
        $hotels = $_GPC['hotels'];
        if (!empty($hotels)) {
            foreach ($hotels as $hotel) {
                $hotel_data = array(
                    'line_id' => $id,
                    'hotel_id' => $hotel
                );
                pdo_insert('bontrip_line_hotel', $hotel_data);
            }
        }
        message('保存线路成功！', $this->createWebUrl('line', array('op' => 'display')), 'success');
    }
    if (!empty($line['thumbs'])) {
        $line['thumbs'] = unserialize($line['thumbs']);
    }
    if (!empty($line['product_desc'])) {
        $line['product_desc'] = unserialize($line['product_desc']);
    }
    $trip_num = empty($line["product_desc"]["trip"])?0:count($line["product_desc"]["trip"]);

    if (!empty($line['price_desc'])) {
        $line['price_desc'] = unserialize($line['price_desc']);
    }
    $price_num = empty($line["price_desc"]["price"])?0:count($line["price_desc"]["price"]);

    if (!empty($line['buy_desc'])) {
        $line['buy_desc'] = unserialize($line['buy_desc']);
    }
    $buy_num = empty($line["buy_desc"]["buy"])?0:count($line["buy_desc"]["buy"]);

    $hotels = pdo_fetchall("SELECT * FROM "
        . tablename('bontrip_hotel') . " WHERE uniacid = '{$uniacid}' and deleted_at is null"
        . " ORDER BY id DESC");

    $line_hotels = pdo_fetchall("SELECT hotel_id FROM "
        . tablename('bontrip_line_hotel') . " WHERE line_id = '{$id}'"
        . " ORDER BY id DESC");
    $line_hotel_array = array();
    if (!empty($line_hotels)) {
        foreach ($line_hotels as $line_hotel) {
            array_push($line_hotel_array, $line_hotel['hotel_id']);
        }
    }

} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $line = pdo_fetch("SELECT id,dest_id  FROM " . tablename('bontrip_line')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");

    if (empty($line)) {
        message('抱歉，线路不存在或是已经被删除！', $this->createWebUrl('line', array('op' => 'display')), 'error');
    }
    pdo_update('bontrip_line', array('deleted_at' => time()), array('id' => $id));

    message('线路删除成功！', $this->createWebUrl('line', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('line', TEMPLATE_INCLUDEPATH, true);